const { userModel, codeModel } = require("../model");
const { createToken } = require("../utils/jwt");

//注册用户的回调
exports.register = async (req, res) => {
  const { username, phone, email, password, code } = req.body;
  if (
    username == undefined ||
    phone == undefined ||
    email == undefined ||
    password == undefined
  ) {
    return res.status(400).json({
      code: 400,
      message: "参数不全",
      data: null,
    });
  }

  //查询数据库中是否已经存在该用户
  const result1 = await userModel.findOne({ phone });
  const result2 = await userModel.findOne({ email });
  if (result1) {
    return res.status(400).json({
      code: 400,
      message: "该手机号已被注册",
      data: null,
    });
  } else if (result2) {
    return res.status(400).json({
      code: 400,
      message: "该邮箱已被注册",
      data: null,
    });
  }
  let isCode = await codeModel.findOne({ email, code });
  if (!isCode) {
    return res.status(400).json({
      code: 400,
      message: "验证码错误",
      data: null,
    });
  }
  //将用户信息插入数据库
  let result = new userModel({
    username,
    phone,
    email,
    password,
  });
  try {
    await result.save();
    res.status(200).json({
      code: 200,
      message: "注册成功",
      data: null,
    });
  } catch (error) {}
};
//用户登录
exports.login = async (req, res) => {
  const { phone, password } = req.body;
  if (phone == undefined || password == undefined) {
    return res.status(400).json({
      code: 400,
      message: "请输入手机号或者密码",
      data: null,
    });
  }

  try {
    let result = await userModel.findOne({ phone, password });
    if (!result) {
        return res.status(400).json({
            code: 400,
            message: "账号或密码错误",
            data: null
        })
    }
    let token = await createToken(result._doc)
    res.status(200).json({
        code:200,
        message:"登录成功",
        data:{
            user:result._doc,
            token
        }
    })
  } catch (error) {
    res.status(500).json({
        code:500,
        message:"服务器错误",
        data:error
    })
  }
};
//获取用户详情
exports.getUserInfo = async (req,res)=>{
  const { id } = req.query
  if(!id){
   return res.status(400).json({
      code:400,
      message:"请输入用户id",
      data:null
   })
  }
  try {
    let result = await userModel.findById(id)
    res.status(200).json({
      code:200,
      message:"获取用户详情成功",
      data:result
    })
  } catch (error) {
    res.status(500).json({
      code:500,
      message:"服务器错误",
      data:error
    })
  }
}
//添加用户余额
exports.addBalance = async (req,res)=>{
  let result = await userModel.findById(req.user._id)
  if(result.money>=50) {
    return res.status(400).json({
      code:400,
      message:"余额充足,无法充值",
      data:null
    })
  }
  result.money = result.money+50
  try {
    await result.save()
    res.status(200).json({
      code:200,
      message:"充值成功",
      data:result
    })
  }catch (error) {
    res.status(500).json({
      code:500,
      message:"服务器错误,充值失败",
      data:error
    })
  }
}
//修改密码
exports.changePassword = async (req,res)=>{
  let result = await userModel.findById(req.user._id)
  const { oldPassword, newPassword } = req.body
  if(result.password!=oldPassword) {
    return res.status(400).json({
      code:400,
      message:"原密码错误",
      data:null
    })
  }
  if(result.password==newPassword) {
    return res.status(400).json({
      code:400,
      message:"新密码不能与原密码相同",
      data:null
    })
  }
  result.password = newPassword
  try {
    await result.save()
    res.status(200).json({
      code:200,
      message:"修改密码成功",
      data:result
    })
  }
  catch (error) {
    res.status(500).json({
      code:500,
      message:"服务器错误,修改密码失败",
      data:error
      
    })
  }
}